package net.oschina.app.hjp1.hjpAdapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import net.oschina.app.Myapp;
import net.oschina.app.R;
import net.oschina.app.hjpbean.DynamicFragmentBean.Tweet;
import net.oschina.app.hjpbean.DynamicFragmentBean.TweetsList;
import net.oschina.app.util.LogUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/7/15 0015.
 */

public class HjpAdapter extends RecyclerView.Adapter<HjpViewHodler> {
    List<Tweet> list;

    public HjpAdapter(List<Tweet> tweetslist) {

        this.list = tweetslist;
    }

    public HjpAdapter() {
        LogUtils.d("11111aaaaaaaaaa1");

        //  initData();
    }

    private void initData() {
        Call<TweetsList> newTweetInfo = Myapp.shopService.getNewTweetInfo(0, 0, 20);
        newTweetInfo.enqueue(new Callback<TweetsList>() {


            private List<Tweet> list;

            @Override
            public void onResponse(Call<TweetsList> call, Response<TweetsList> response) {
                //  List<Tweet> tweetslist = response.body().getTweetslist();

                int pagesize = response.body().getPagesize();
                list = response.body().getList();

                LogUtils.d("222222222222222222222222222222");

                LogUtils.d(pagesize + "");

                LogUtils.d(list.get(0).getAuthor() + "");


                LogUtils.d(HjpAdapter.this.list.size() + "`````````````````````");

            }

            @Override
            public void onFailure(Call<TweetsList> call, Throwable t) {
                t.printStackTrace();

            }
        });


    }


    @Override
    public HjpViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        LogUtils.d("onCreateViewHolder");

        View inflate = LayoutInflater.from(Myapp.mApp).inflate(R.layout.hjp_recyclerview_item, parent,
                false);


        HjpViewHodler hodler = new HjpViewHodler(inflate);

        return hodler;
    }

    @Override
    public void onBindViewHolder(HjpViewHodler holder, int position) {
        Tweet tweet = list.get(position);
        LogUtils.d("onBindViewHolder");

        Picasso.with(Myapp.getContext()).load(tweet.getPortrait()).into(holder.iv);
                 holder.tv.setText(tweet.getAuthor());  //作者
                holder.tv1.setText(tweet.getBody());  //内容
                holder.tv2.setText(tweet.getPubDate());  //内容

//        InputHelper.backspace();



         //        holder.tv3.setText("22223333333333333333333333333333333222222222222222222");

        Toast.makeText(Myapp.getContext(), "" + list.size(), Toast.LENGTH_SHORT).show();


    }

    @Override
    public int getItemCount() {

        Log.i("Tag", list.size() + "");
        return list == null ? 0 : list.size();


    }

}


class HjpViewHodler extends RecyclerView.ViewHolder {

    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;

    @BindView(R.id.iv)
    ImageView iv;
    @BindView(R.id.iv1)
    ImageView iv1;
    @BindView(R.id.iv2)
    ImageView iv2;
    //
    //    public final TextView tt;

    public HjpViewHodler(View itemView) {
        super(itemView);
        ButterKnife.bind(this,itemView);
        //        tt = (TextView) itemView.findViewById(R.id.tv);
    }
}
